﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;
public partial class XacNhanDatHang : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnPrevious_Click(object sender, EventArgs e)
    {
        Response.Redirect("GioHang.aspx");
    }
    protected void btnContDatHang_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int dem = 0;
        DTO.GioHangDTO GioHang = (DTO.GioHangDTO)Session["GioHang"];
        if (GioHang == null)
            Response.Redirect("GioHang.aspx");
        for (int i = 0; i < GioHang.lstHang.Count; i++)
        {
            DTO.HoaDonDTO hoadon = new HoaDonDTO(0, 0, 0, 0, DateTime.Now, DateTime.Now, DateTime.Now);
            hoadon.ThanhVienMuaHang = GioHang.IDNguoiDung;
            hoadon.ThoiGianLapHoaDon = DateTime.Now;
            hoadon.ThoiGianChuyenHang = DateTime.Now;
            hoadon.ThoiGianThanhToan = DateTime.Now;
            hoadon.MaGianHang = GioHang.Gianhangvaomua[i];
            hoadon.TinhTrangHoaDon = 0;

            hoadon.TongSoTienThanhToan = GioHang.ThanhTienTenSanPham[i];
            long soluong = GioHang.ThanhTienTenSanPham[i] / GioHang.lstHang[i].GiaBanHienTai;
            bool result = BUS.HoaDonBUS.ThemHoaDon(hoadon);            
            if (result == true)
            {
                ChiTietHoaDonDTO cthd = new ChiTietHoaDonDTO(GioHang.lstHang[i].ID,BUS.HoaDonBUS.LayHoaDonCuoiCung().ID,GioHang.lstHang[i].GiaBanHienTai,int.Parse(soluong.ToString()));
                if (BUS.ChiTietHoaDonBUS.ThemChiTietHoaDon(cthd) == true)
                {
                    dem++;    
                }
                
            }
        }


        if (dem != GioHang.lstHang.Count)
            Response.Redirect("KetQuaDatHang.aspx?failed=1");
        Response.Redirect("KetQuaDatHang.aspx?success=1");
    }
}
